package org.cloud.nacos.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 *public class ConsumerController{ }
 * @author ASDFGH
 * @date 2021年1月6日 下午4:57:07
 */
@RestController
public class ConsumerController {
	@Autowired
	private LoadBalancerClient client;
	@Autowired
	private RestTemplate restTemplate;
	@Value("${spring.application.name}")
	private String appName;

	@GetMapping(value = "/test/app/name")
	public String test() {
		//使用LoadbanceClient来进行访问
		ServiceInstance instance = client.choose("nacos-provider");
		String url = String.format("http://%s:%s/test/%s", instance.getHost(), instance.getPort(), appName);
		return restTemplate.getForObject(url, String.class);
	}
}
